package com.sskj.phoenixcoin.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.phoenixcoin.MainActivity;
import com.sskj.phoenixcoin.R;
import com.sskj.phoenixcoin.bean.AppBank;
import com.sskj.phoenixcoin.component.DaggerUserDataComponent;
import com.sskj.phoenixcoin.presenter.AppBindBankActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AppBindBankActivity extends BaseActivity<AppBindBankActivityPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.bank_img)
    ImageView bankImg;
    @BindView(R.id.bank_name)
    TextView bankName;
    @BindView(R.id.bank_branch)
    TextView bankBranch;
    @BindView(R.id.bank_num)
    TextView bankNum;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.bank_show_ll)
    LinearLayout bankShowLl;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private String bankId;
    private AppBank appBankEdit;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_bind_activity_bank;
    }

    @Override
    public AppBindBankActivityPresenter getPresenter() {
        return new AppBindBankActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        rightTv.setVisibility(View.VISIBLE);
        rightTv.setText("添加");
        tvTitle.setText("绑定银行卡");
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mPresenter.getBank(String.valueOf(userData.getId()), null);
            } else {
                userData = null;
            }
        });
        rightTv.setVisibility(View.VISIBLE);
        ClickUtil.click(rightTv, () -> {
            AppAddBankActivity.start(this, appBankEdit != null ? true : false, appBankEdit);
        });
        ClickUtil.click(ivBack, () -> {
            finish();
        });
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                if (userData != null) {
                    mPresenter.getBank(String.valueOf(userData.getId()), coolRefreshView);
                } else {
                    coolRefreshView.setRefreshing(false);
                }
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mPresenter.getBank(String.valueOf(userData.getId()), null);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AppBindBankActivity.class);
        context.startActivity(intent);
    }

    public void successUi(AppBank appBank) {
        if (appBank != null) {
            bankShowLl.setVisibility(View.VISIBLE);
            appBankEdit = appBank;
            bankId = appBank.getId();
            rightTv.setText("编辑");
            bankName.setText(appBank.getBankCardType());
            bankBranch.setText(appBank.getBankCardOpenBank());
            bankNum.setText(HideUtil.getBankNumHide(appBank.getBankCardNo()));
        } else {
            bankShowLl.setVisibility(View.GONE);
            rightTv.setText("添加");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (userData != null) {
            mPresenter.getBank(String.valueOf(userData.getId()), null);
        }
    }
}
